package com.qiantu.phone.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewConfiguration;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class SectionSeekBar extends View {

    private float mTouchDownX1;
    private float mTouchDownX2;
    private float mTouchDownY1;
    private float mTouchDownY2;
    private int mActivePointerId1 = -1;
    private int mActivePointerId2 = -1;
    private int mScaledTouchSlop;
    private Drawable progressTextBg;
    private String unit = "%";
    // 画笔
    private Paint mPaint;
    private Paint mTextPaint;
    private Paint mThumbPaint;
    // 滑块按钮宽度
    private float mThumbWidth;
    private float mThumbStrokeWidth;
    private float mThumbHeight;
    private float progressTextSize;
    private int progressTextColor;
    private float progressTextMarginTop;
    private float progressTextBgMarginBottom;
    private float leftRightTextMarginTop;
    private float progressTrackHeight;
    private float leftRightTextSize;
    private int leftRightTextColor;
    private int progressTextHeight;
    private int progressTextHolderHeight;
    private int bottomLeftRightTextHeight;
    private int bottomLeftRightTextHolderHeight;
    private String progressText1 = null;
    private String progressText2 = null;
    private float progress1;
    private float progress2;
    private float canSlideTotalWidth;
    private OnSeekBarChangeListener mOnSeekBarChangeListener;
    private float thumbCx1;
    private float thumbCx2;
    private boolean isLeftDrag = false;
    private boolean isRightDrag = false;
    private float slidDistance1;
    private float slidDistance2;
    private boolean isFirst;
    private RectF mBarRectF;
    private RectF mProgressBarRectF;
    private RectF mThumbRectF;
    private int[] gradientColors = new int[]{0xff76bafd, 0xffffa273};
    private float mThumbLeftRightOffset;
    //可拖范围加个偏移
    private int mThumbDragOffset;
    private int minValue;
    private int maxValue;
    private float totalProgress;
    private int mValue1;
    private int mValue2;
    private int backgroundColor;

    public SectionSeekBar(Context context) {
        this(context, null);
    }

    public SectionSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SectionSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = attrs == null ? null : getContext().obtainStyledAttributes(attrs, R.styleable.TemperatureSeekBar);
        if (ta != null) {
            mThumbWidth = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_thumb_width, dp2px(26));
            mThumbHeight = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_thumb_height, dp2px(26));
            progressTrackHeight = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_track_height, dp2px(12));
            progressTextSize = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_text_size, dp2px(12));
            progressTextColor = ta.getColor(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_text_color, AppApplication.getInstance().getThemeColor(R.attr.textColor4));
            progressTextBg = ta.getDrawable(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_text_bg);
            minValue = ta.getInt(R.styleable.TemperatureSeekBar_temperature_seek_bar_min, 0);
            maxValue = ta.getInt(R.styleable.TemperatureSeekBar_temperature_seek_bar_max, 100);
            leftRightTextSize = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_left_right_text_size, dp2px(10));
            leftRightTextColor = ta.getColor(R.styleable.TemperatureSeekBar_temperature_seek_bar_left_right_text_color, AppApplication.getInstance().getThemeColor(R.attr.textColor2));
            progressTextMarginTop = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_text_margin_top, dp2px(11));
            progressTextBgMarginBottom = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_progress_text_bg_margin_bottom, dp2px(8));
            leftRightTextMarginTop = ta.getDimension(R.styleable.TemperatureSeekBar_temperature_seek_bar_left_right_text_margin_top, dp2px(4));
            unit = ta.getString(R.styleable.TemperatureSeekBar_temperature_seek_bar_unit);
        }
        backgroundColor = AppApplication.getInstance().getThemeColor(R.attr.secondaryColor9);
        mThumbStrokeWidth = dp2px(4);
        mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        mBarRectF = new RectF();
        mProgressBarRectF = new RectF();
        mThumbStrokeWidth = getResources().getDimensionPixelSize(R.dimen.dp_2);
        mThumbDragOffset = getResources().getDimensionPixelSize(R.dimen.dp_10);
        getTotalProgress();
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);

        mTextPaint = new TextPaint();
        mTextPaint.setAntiAlias(true);

        mThumbPaint = new Paint();
        mThumbPaint.setStrokeWidth(mThumbStrokeWidth);
        mThumbPaint.setAntiAlias(true);

        if (progressTextBg != null && progressTextBg.getIntrinsicWidth() != 0 && progressTextBg.getIntrinsicHeight() != 0) {
            progressTextHolderHeight = (int) (progressTextBg.getIntrinsicHeight() + progressTextBgMarginBottom);
            int progressTextHolderWidth = progressTextBg.getIntrinsicWidth();
            mThumbLeftRightOffset = progressTextHolderWidth / 2f;
        } else {
            mTextPaint.setTextSize(progressTextSize);
            Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
            progressTextHeight = (int) (fontMetrics.bottom - fontMetrics.top);
            progressTextHolderHeight = (int) (progressTextHeight + progressTextBgMarginBottom);
            int progressTextHolderWidth = (int) Math.max(mTextPaint.measureText(minValue + unit), mTextPaint.measureText(maxValue + unit));
            mThumbLeftRightOffset = progressTextHolderWidth / 2f;
        }
        mTextPaint.setTextSize(leftRightTextSize);
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        bottomLeftRightTextHeight = (int) (fontMetrics.bottom - fontMetrics.top);
        bottomLeftRightTextHolderHeight = (int) (bottomLeftRightTextHeight + leftRightTextMarginTop);

        mThumbRectF = new RectF();
    }

    public void setMinMaxValue(int minValue, int maxValue) {
        this.minValue = minValue;
        this.maxValue = maxValue;
        getTotalProgress();
    }

    private void getTotalProgress() {
        totalProgress = (maxValue - minValue);
    }

    public void setGradientColors(int[] gradientColors) {
        this.gradientColors = gradientColors;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }
        int index = -1;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                isFirst = true;
                index = event.getActionIndex();
                float downX = event.getX(index);
                float downY = event.getY(index);
                mTouchDownX1 = -1;
                mTouchDownY1 = -1;
                mTouchDownX2 = -1;
                mTouchDownY2 = -1;
                mActivePointerId1 = -1;
                mActivePointerId2 = -1;
                //只有Thumb区域可以点击拖动
                if (downX >= thumbCx1 - mThumbWidth / 2f - mThumbDragOffset && downX <= thumbCx1 + mThumbWidth / 2f + mThumbDragOffset
                        && downY >= progressTextHolderHeight && downY - mThumbDragOffset <= progressTextHolderHeight + mThumbHeight + mThumbDragOffset) {
                    //圆点之内可以拖动
                    isLeftDrag = true;
                    isRightDrag = false;
                    mTouchDownX1 = downX;
                    mTouchDownY1 = downY;
                    mActivePointerId1 = event.getPointerId(index);
                    //已滑动距离
                    slidDistance1 = progress1 * canSlideTotalWidth;
                } else if (downX >= thumbCx2 - mThumbWidth / 2f - mThumbDragOffset && downX <= thumbCx2 + mThumbWidth / 2f + mThumbDragOffset
                        && downY >= progressTextHolderHeight && downY - mThumbDragOffset <= progressTextHolderHeight + mThumbHeight + mThumbDragOffset) {
                    //圆点之内可以拖动
                    isLeftDrag = false;
                    isRightDrag = true;
                    mTouchDownX2 = downX;
                    mTouchDownY2 = downY;
                    mActivePointerId2 = event.getPointerId(index);
                    //已滑动距离
                    slidDistance2 = progress2 * canSlideTotalWidth;
                } else {
                    //圆点之外只能点击
                    isLeftDrag = false;
                    isRightDrag = false;
                    //点击分左右边
                    if (downX <= getWidth() / 2f) {
                        mTouchDownX1 = downX;
                        mTouchDownY1 = downY;
                        //已滑动距离
                        slidDistance1 = progress1 * canSlideTotalWidth;
                        mActivePointerId1 = event.getPointerId(index);
                    } else {
                        mTouchDownX2 = downX;
                        mTouchDownY2 = downY;
                        //已滑动距离
                        slidDistance2 = progress2 * canSlideTotalWidth;
                        mActivePointerId2 = event.getPointerId(index);
                    }
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                index = event.getActionIndex();
                float pointerDownX = event.getX(index);
                float pointerDownY = event.getY(index);
                //只有Thumb区域可以点击拖动
                if (pointerDownX >= thumbCx1 - mThumbWidth / 2f - mThumbDragOffset && pointerDownX <= thumbCx1 + mThumbWidth / 2f + mThumbDragOffset
                        && pointerDownY >= progressTextHolderHeight && pointerDownY - mThumbDragOffset <= progressTextHolderHeight + mThumbHeight + mThumbDragOffset) {
                    //圆点之内可以拖动
                    isLeftDrag = true;
                    isRightDrag = false;
                    mTouchDownX1 = pointerDownX;
                    mTouchDownY1 = pointerDownY;
                    mActivePointerId1 = event.getPointerId(index);
                    //已滑动距离
                    slidDistance1 = progress1 * canSlideTotalWidth;
                } else if (pointerDownX >= thumbCx2 - mThumbWidth / 2f - mThumbDragOffset && pointerDownX <= thumbCx2 + mThumbWidth / 2f + mThumbDragOffset
                        && pointerDownY >= progressTextHolderHeight && pointerDownY - mThumbDragOffset <= progressTextHolderHeight + mThumbHeight + mThumbDragOffset) {
                    //圆点之内可以拖动
                    isLeftDrag = false;
                    isRightDrag = true;
                    mTouchDownX2 = pointerDownX;
                    mTouchDownY2 = pointerDownY;

                    mActivePointerId2 = event.getPointerId(index);
                    //已滑动距离
                    slidDistance2 = progress2 * canSlideTotalWidth;
                } else {
                    //圆点之外只能点击
                    isLeftDrag = false;
                    isRightDrag = false;
                    //点击分左右边
                    if (pointerDownX <= getWidth() / 2f) {
                        mTouchDownX1 = pointerDownX;
                        mTouchDownY1 = pointerDownY;
                        //已滑动距离
                        slidDistance1 = progress1 * canSlideTotalWidth;
                        mActivePointerId1 = event.getPointerId(index);
                    } else {
                        mTouchDownX2 = pointerDownX;
                        mTouchDownY2 = pointerDownY;
                        //已滑动距离
                        slidDistance2 = progress2 * canSlideTotalWidth;
                        mActivePointerId2 = event.getPointerId(index);
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (isLeftDrag || isRightDrag) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    int index1 = event.findPointerIndex(mActivePointerId1);
                    int index2 = event.findPointerIndex(mActivePointerId2);
                    if (index1 != -1 && index2 != -1) {
                        float distance = event.getX(index1) - mTouchDownX1;
                        progress1 = (distance + slidDistance1) / canSlideTotalWidth;
                        progress1 = progress1 < 0 ? 0 : progress1;
                        progress1 = progress1 >= 0.995 ? 1 : progress1;
                        Log.i("luzx", "progress1:" + progress1);

                        distance = event.getX(index2) - mTouchDownX2;
                        progress2 = (distance + slidDistance2) / canSlideTotalWidth;
                        progress2 = progress2 < 0 ? 0 : progress2;
                        progress2 = progress2 >= 0.995 ? 1 : progress2;
                        Log.i("luzx", "progress2:" + progress2);
                    } else {
                        if (index1 != -1) {
                            if (mTouchDownX1 != -1) {
                                float distance = event.getX(index1) - mTouchDownX1;
                                progress1 = (distance + slidDistance1) / canSlideTotalWidth;
                                progress1 = progress1 < 0 ? 0 : progress1;
                                progress1 = progress1 >= 0.995 ? 1 : progress1;
                                Log.i("luzx", "progress1:" + progress1);
                            }
                        }
                        if (index2 != -1) {
                            if (mTouchDownX2 != -1) {
                                float distance = event.getX(index2) - mTouchDownX2;
                                progress2 = (distance + slidDistance2) / canSlideTotalWidth;
                                progress2 = progress2 < 0 ? 0 : progress2;
                                progress2 = progress2 >= 0.995 ? 1 : progress2;
                                Log.i("luzx", "progress2:" + progress2);
                            }
                        }
                    }
                    calculateValue();
                    invalidate();
                    if (mOnSeekBarChangeListener != null) {
                        if (isFirst) {
                            isFirst = false;
                            mOnSeekBarChangeListener.onStartTrackingTouch();
                        }
                        mOnSeekBarChangeListener.onValueChanged(Math.min(mValue1, mValue2), Math.max(mValue1, mValue2));
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                //点击
                int index1 = event.findPointerIndex(mActivePointerId1);
                int index2 = event.findPointerIndex(mActivePointerId2);
                if (index1 != -1 && index2 != -1) {
                    float clickDistance1 = (float) Math.sqrt(Math.pow(Math.abs(event.getX(index1) - mTouchDownX1), 2) + Math.pow(Math.abs(event.getY(index1) - mTouchDownY1), 2));
                    if (clickDistance1 <= dp2px(2)) {
                        progress1 = (event.getX(index1) - mThumbLeftRightOffset) / canSlideTotalWidth;
                        progress1 = progress1 < 0 ? 0 : progress1;
                        progress1 = progress1 >= 0.995 ? 1 : progress1;
                        Log.i("luzx", "progress1:" + progress1);
                    }
                    float clickDistance2 = (float) Math.sqrt(Math.pow(Math.abs(event.getX(index2) - mTouchDownX2), 2) + Math.pow(Math.abs(event.getY(index2) - mTouchDownY2), 2));
                    if (clickDistance2 <= dp2px(2)) {
                        progress2 = (event.getX(index2) - mThumbLeftRightOffset) / canSlideTotalWidth;
                        progress2 = progress2 < 0 ? 0 : progress2;
                        progress2 = progress2 >= 0.995 ? 1 : progress2;
                        Log.i("luzx", "progress2:" + progress2);
                    }
                    calculateValue();
                    invalidate();
                    if (mOnSeekBarChangeListener != null) {
                        playSoundEffect(SoundEffectConstants.CLICK);
                        mOnSeekBarChangeListener.onStopTrackingTouch(Math.min(mValue1, mValue2), Math.max(mValue1, mValue2));
                    }
                } else {
                    if (index1 != -1) {
                        float clickDistance1 = (float) Math.sqrt(Math.pow(Math.abs(event.getX(index1) - mTouchDownX1), 2) + Math.pow(Math.abs(event.getY(index1) - mTouchDownY1), 2));
                        if (clickDistance1 <= dp2px(2)) {
                            progress1 = (event.getX(index1) - mThumbLeftRightOffset) / canSlideTotalWidth;
                            progress1 = progress1 < 0 ? 0 : progress1;
                            progress1 = progress1 >= 0.995 ? 1 : progress1;
                            Log.i("luzx", "progress1:" + progress1);
                            calculateValue();
                            invalidate();
                            if (mOnSeekBarChangeListener != null) {
                                playSoundEffect(SoundEffectConstants.CLICK);
                                mOnSeekBarChangeListener.onStopTrackingTouch(Math.min(mValue1, mValue2), Math.max(mValue1, mValue2));
                            }
                        }
                    }
                    if (index2 != -1) {
                        float clickDistance2 = (float) Math.sqrt(Math.pow(Math.abs(event.getX(index2) - mTouchDownX2), 2) + Math.pow(Math.abs(event.getY(index2) - mTouchDownY2), 2));
                        if (clickDistance2 <= dp2px(2)) {
                            progress2 = (event.getX(index2) - mThumbLeftRightOffset) / canSlideTotalWidth;
                            progress2 = progress2 < 0 ? 0 : progress2;
                            progress2 = progress2 >= 0.995 ? 1 : progress2;
                            Log.i("luzx", "progress2:" + progress2);
                            calculateValue();
                            invalidate();
                            if (mOnSeekBarChangeListener != null) {
                                playSoundEffect(SoundEffectConstants.CLICK);
                                mOnSeekBarChangeListener.onStopTrackingTouch(Math.min(mValue1, mValue2), Math.max(mValue1, mValue2));
                            }
                        }
                    }
                }
                getParent().requestDisallowInterceptTouchEvent(false);
                break;
            case MotionEvent.ACTION_CANCEL:
//                getParent().requestDisallowInterceptTouchEvent(false);
//                if (mOnSeekBarChangeListener != null) {
//                    mOnSeekBarChangeListener.onStopTrackingTouch(progressStep100);
//                }
//                setAlpha(1f);
                break;
        }
        return true;
    }

    private void calculateValue() {
        mValue1 = (int) Math.round(progress1 * totalProgress) + minValue;
        mValue1 = mValue1 < minValue ? minValue : mValue1;
        mValue1 = mValue1 > maxValue ? maxValue : mValue1;

        mValue2 = (int) Math.round(progress2 * totalProgress) + minValue;
        mValue2 = mValue2 < minValue ? minValue : mValue2;
        mValue2 = mValue2 > maxValue ? maxValue : mValue2;
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        mPaint.setColor(backgroundColor);
        canvas.drawRoundRect(mBarRectF, progressTrackHeight, progressTrackHeight, mPaint);

        thumbCx1 = mThumbLeftRightOffset + (canSlideTotalWidth * progress1);
        thumbCx2 = mThumbLeftRightOffset + (canSlideTotalWidth * progress2);

        mProgressBarRectF.left = thumbCx1;
        mProgressBarRectF.right = thumbCx2;
        mPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        canvas.drawRect(mProgressBarRectF, mPaint);

        mThumbPaint.setColor(0xffffffff);
        mThumbPaint.setStyle(Paint.Style.FILL);
        mThumbRectF.left = thumbCx1 - mThumbWidth / 2f + (mThumbStrokeWidth / 2f);
        mThumbRectF.top = progressTextHolderHeight + (mThumbStrokeWidth / 2f);
        mThumbRectF.right = thumbCx1 + mThumbWidth / 2f - (mThumbStrokeWidth / 2f);
        mThumbRectF.bottom = mThumbRectF.top + mThumbHeight - (mThumbStrokeWidth / 2f);
        canvas.drawRoundRect(mThumbRectF, mThumbWidth, mThumbWidth, mThumbPaint);
        mThumbPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        mThumbPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(mThumbRectF, mThumbWidth, mThumbWidth, mThumbPaint);

        int progressTextBgLeft1 = (int) (thumbCx1 - progressTextBg.getIntrinsicWidth() / 2);
        int progressTextBgRight1 = (int) (thumbCx1 + progressTextBg.getIntrinsicWidth() / 2);
        if (progressTextBg != null) {
            progressTextBg.setBounds(progressTextBgLeft1, 0, progressTextBgRight1, progressTextBg.getIntrinsicHeight());
        }
        progressTextBg.draw(canvas);

        mThumbPaint.setColor(0xffffffff);
        mThumbPaint.setStyle(Paint.Style.FILL);
        mThumbRectF.left = thumbCx2 - mThumbWidth / 2f + (mThumbStrokeWidth / 2f);
        mThumbRectF.top = progressTextHolderHeight + (mThumbStrokeWidth / 2f);
        mThumbRectF.right = thumbCx2 + mThumbWidth / 2f - (mThumbStrokeWidth / 2f);
        mThumbRectF.bottom = mThumbRectF.top + mThumbHeight - (mThumbStrokeWidth / 2f);
        canvas.drawRoundRect(mThumbRectF, mThumbWidth, mThumbWidth, mThumbPaint);
        mThumbPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.themeColor));
        mThumbPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(mThumbRectF, mThumbWidth, mThumbWidth, mThumbPaint);

        int progressTextBgLeft2 = (int) (thumbCx2 - progressTextBg.getIntrinsicWidth() / 2);
        int progressTextBgRight2 = (int) (thumbCx2 + progressTextBg.getIntrinsicWidth() / 2);
        if (progressTextBg != null) {
            progressTextBg.setBounds(progressTextBgLeft2, 0, progressTextBgRight2, progressTextBg.getIntrinsicHeight());
        }
        progressTextBg.draw(canvas);

        mValue1 = mValue1 == 0 ? minValue : mValue1;
        progressText1 = mValue1 + unit;
        mTextPaint.setTextSize(progressTextSize);
        mTextPaint.setColor(progressTextColor);
        int textX1 = (int) (thumbCx1 - mTextPaint.measureText(progressText1) / 2);
        canvas.drawText(progressText1, textX1, -mTextPaint.getFontMetrics().ascent + progressTextMarginTop, mTextPaint);

        mValue2 = mValue2 == 0 ? minValue : mValue2;
        progressText2 = mValue2 + unit;
        mTextPaint.setTextSize(progressTextSize);
        mTextPaint.setColor(progressTextColor);
        int textX2 = (int) (thumbCx2 - mTextPaint.measureText(progressText2) / 2);
        canvas.drawText(progressText2, textX2, -mTextPaint.getFontMetrics().ascent + progressTextMarginTop, mTextPaint);

        mTextPaint.setTextSize(leftRightTextSize);
        mTextPaint.setColor(leftRightTextColor);
        String leftText = minValue + unit;
        canvas.drawText(leftText, mThumbLeftRightOffset, getHeight() - mTextPaint.getFontMetrics().descent, mTextPaint);

        String rightText = maxValue + unit;
        canvas.drawText(rightText, getWidth() - mTextPaint.measureText(rightText) - mThumbLeftRightOffset, getHeight() - mTextPaint.getFontMetrics().descent, mTextPaint);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int dw = getMeasuredWidth();
        int dh = (int) (progressTextHolderHeight + mThumbHeight + bottomLeftRightTextHolderHeight);
        setMeasuredDimension(dw, dh);
        mBarRectF.left = mThumbLeftRightOffset;
        mBarRectF.right = dw - mThumbLeftRightOffset;
        mBarRectF.top = progressTextHolderHeight + (mThumbHeight - progressTrackHeight) / 2f;
        mBarRectF.bottom = mBarRectF.top + progressTrackHeight;
        mProgressBarRectF.top = mBarRectF.top;
        mProgressBarRectF.bottom = mBarRectF.bottom;
        canSlideTotalWidth = mBarRectF.width();
    }

//    public void setProgress(int progress) {
//        progress = progress > 100 ? 100 : progress;
//        this.progress = progress / 100f;
//        progressStep100 = (int) (this.progress * totalProgress + 0.5f);
//        invalidate();
//    }

    public void setValue(int value1, int value2) {
        mValue1 = value1;
        progress1 = (Float.valueOf(mValue1) - minValue) / totalProgress;
        progress1 = progress1 < 0 ? 0 : progress1;
        progress1 = progress1 > 1 ? 1 : progress1;

        mValue2 = value2;
        progress2 = (Float.valueOf(mValue2) - minValue) / totalProgress;
        progress2 = progress2 < 0 ? 0 : progress2;
        progress2 = progress2 > 1 ? 1 : progress2;
        invalidate();
    }

    public int getValue1() {
        return mValue1;
    }

    public int getValue2() {
        return mValue2;
    }

    public void setUnit(String unit) {
        this.unit = unit;
        invalidate();
    }

    /**
     * 进度监听
     */
    public interface OnSeekBarChangeListener {
        /**
         * 进度监听回调
         *
         * @param beginValue
         * @param endValue
         */
        public void onValueChanged(int beginValue, int endValue);

        /**
         * 开始拖动
         */
        public void onStartTrackingTouch();

        /**
         * 停止拖动
         */
        public void onStopTrackingTouch(int beginValue, int endValue);
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener changeListener) {
        this.mOnSeekBarChangeListener = changeListener;
    }

    /**
     * dp转px
     *
     * @param dp dp值
     * @return px值
     */
    public int dp2px(float dp) {
        return (int) (getResources().getDimension(R.dimen.dp_1) * dp);
    }
}
